package com.dwb.service;

import com.alibaba.fastjson.JSON;
import com.dwb.config.BizException;
import com.dwb.dto.SJishiDto;
import com.dwb.entity.SJishi;
import com.dwb.entity.SJishiZizhi;
import com.dwb.mappers.AptitudeMapper;
import com.dwb.mappers.TechnicianMapper;
import com.dwb.vo.PageVo;
import com.dwb.vo.SJishiZizhiVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.beans.Transient;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.dwb.common.check.Check.*;
import static com.dwb.common.check.Check.CHECK_TECHNICIAN_GET_INFO;

/**
 * @Author manke
 * @Date 2020/6/23 16:18
 * @Version 0.0.1
 */
@Service
public class TechnicianService {

    @Autowired
    private TechnicianMapper technicianMapper;
    @Autowired
    private AptitudeMapper aptitudeMapper;

    public void insert(SJishi technician) {
        CHECK_TECHNICIAN(technician); //校验
//        technicianMapper.insert(technician);


    }

    public PageInfo<SJishi> getList(Map<String, Object> parameters) {


        PageHelper.startPage(Integer.parseInt(parameters.get("pageNum").toString() == null ? "" : "1"),
                Integer.parseInt(parameters.get("pageSize").toString() == null ? "" : "10"));
        Example example = new Example(SJishi.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", parameters.get("status"));
        criteria.andEqualTo("address", parameters.get("address"));
        criteria.andLike("name", "%" + parameters.get("name") + "%");
        List<SJishi> sJishis = technicianMapper.selectByExample(example);
        return new PageInfo<>(sJishis);


    }

    public Map<String, Object> getInfo(Map<String, String> parameters) {
        CHECK_TECHNICIAN_GET_INFO(parameters);//校验
        SJishi sJishi = technicianMapper.selectByPrimaryKey(parameters.get("id"));
        Example example = new Example(SJishiZizhi.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("jishiId", sJishi.getId());
        List<SJishiZizhi> sJishiZizhis = aptitudeMapper.selectByExample(example);
        HashMap<String, Object> map = new HashMap<>();
        map.put("technician", sJishi);
        map.put("aptitude", sJishiZizhis);
        return map;
    }
//aptitude

    @Transactional
    public void updateInfo(Map<String, Object> parameters) {
        CHECK_TECHNICIAN_UPDATE_INFO(parameters); //校验
        Object technician = parameters.get("technician");  // 技师
        ObjectMapper objectMapper = new ObjectMapper();
        SJishi sJishi = objectMapper.convertValue(technician, SJishi.class);
        List<Object> aptitudes = (List<Object>) parameters.get("aptitude"); // 技师资质
        technicianMapper.updateByPrimaryKeySelective(sJishi);
        Example example = new Example(SJishiZizhi.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("jishiId", sJishi.getId());
        aptitudeMapper.deleteByExample(example);
        for (int i = 0; i < aptitudes.size(); i++) {
            Object o = aptitudes.get(i);
            SJishiZizhi sJishiZizhi1 = objectMapper.convertValue(o, SJishiZizhi.class);
            aptitudeMapper.insert(sJishiZizhi1);
        }
    }

    public void updateState(Map<String, String> parameters) {
        SJishi sJishi = new SJishi();
        sJishi.setId(Integer.parseInt(parameters.get("id")));
        sJishi.setStatus((Integer.parseInt(parameters.get("state"))==0 ? false : true));
        technicianMapper.updateByPrimaryKeySelective(sJishi);
    }
}
